<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Course extends Model
{
    //软删除
    use SoftDelete;

    //課程添加
    public function course_add($data)
    {
        $validate = new \app\common\validate\Course();
        if (!$validate -> check($data)){
            return $validate -> getError();
        }
        $info = $data['img'] -> move(ROOT_PATH.'public'.DS.'uploads/course');
        if ($info){
            $data['img'] = '/uploads/course/'.$info -> getSaveName();
        } else {
            return $data -> getError();
        }
        $result = $this -> save($data);
        if ($result){
            return 1;
        } else {
            return '添加成功！';
        }
    }

    //課程編輯
    public function course_edit($data)
    {

        $validate = new \app\common\validate\Course();
        if ($data['imgs']){
            $data['img'] = $data['imgs'];
            if (!$validate->check($data)){
                return $validate -> getError();
            }
            $info = $data['imgs'] -> move(ROOT_PATH.'public'.DS.'uploads/course');
            if ($info){
                $data['img'] = '/uploads/course/'.$info -> getSaveName();
            } else {
                return $data -> getError();
            }
        } else {
            if ($validate -> scene('edit') -> check($data)){
                return $validate -> getError();
            }
        }
        $cou = $this -> find($data['id']);
        $cou -> img = $data['img'];
        $cou -> cname = $data['cname'];
        $cou -> score = $data['score'];
        $cou -> account = $data['account'];
        $cou -> details = $data['details'];
        $result = $cou -> save();
        if ($result){
            return 1;
        } else {
            return '更新失败！';
        }

    }


}
